import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter_section_table_view/flutter_section_table_view.dart';
import 'package:ls_work/a_yyt/models/pending_selected_group_entity.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/styles.dart';

import '../../util/flat_button.dart';


// import 'package:ls_work/util/toast.dart';

class PendingSingleStaffPage extends StatefulWidget {
  List<PendingSelectedGroupEntity> orginList;
  final Function(
    List<PendingSelectedGroupInfo>,
  )? callBack;
  PendingSingleStaffPage({Key? key, required this.orginList, this.callBack});
  @override
  _PendingSingleStaffPageState createState() => _PendingSingleStaffPageState();
}

class _PendingSingleStaffPageState extends State<PendingSingleStaffPage> {
  List<PendingSelectedGroupInfo> selectedList = [];

  @override
  void initState() {
    super.initState();
    for (var item in widget.orginList) {
      for (var item2 in (item.user??<PendingSelectedGroupInfo>[])) {
        if (item2.isSelected != null && item2.isSelected==true) {
          selectedList.add(item2);
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('选择'),
        elevation: 0.5,
        actions: <Widget>[
          FlatsButton(
            onPressed: () {
              // if (selectedList.length <= 0) {
              //   Toast.show('请选择组员');
              //   return;
              // }
              if(widget.callBack!=null){
                widget.callBack!(selectedList);
              }
              Navigator.pop(context);
            },
            child: Text(
              '确定',
              style: TextStyle(
                color: Colours.app_main,
                fontSize: 18,
              ),
            ),
          )
        ],
      ),
      body: SectionTableView(
        sectionCount: widget.orginList.length,
        numOfRowInSection: (section) => (widget.orginList[section].user??[]).length,
        cellAtIndexPath: (section, row) {
          return Container(
            height: 60.0,
            padding: EdgeInsets.only(left: 30, right: 30),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  ' ${widget.orginList[section].user![row].userName}',
                  style: TextStyles.textDark18,
                ),
                Checkbox(
                  value: widget.orginList[section].user![row].isSelected != null
                      ? widget.orginList[section].user![row].isSelected
                      : false,
                  onChanged: (value) {
                    setState(() {
                      widget.orginList[section].user![row].isSelected = value;
                      if (value==true) {
                        selectedList.add(widget.orginList[section].user![row]);
                      } else {
                        selectedList
                            .remove(widget.orginList[section].user![row]);
                      }
                    });
                  },
                )
              ],
            ),
          );
        },
        headerInSection: (section) {
          return Container(
            height: 64.0,
            padding: EdgeInsets.only(left: 30),
            color: Colours.bg_color,
            alignment: Alignment.centerLeft,
            child: Text(
              ' ${widget.orginList[section].name}',
              style: TextStyles.textDark20,
            ),
          );
        },
        divider: Container(
          color: Colours.line,
          height: 1.0,
        ),
      ),
    );
  }
}
